﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;

namespace ReklamaWPF.Grids
{
    /// <summary>
    /// Interaction logic for BlocksGrid.xaml
    /// </summary>
    public partial class BlocksGrid : UserControl
    {
        DataBase db = new DataBase("192.168.100.100\\SQLEXPRESS", "sa", "tornado", "PASystem");

        public BlocksGrid()
        {
            InitializeComponent();
            blocksGridControl.View.ShowGroupPanel = false;
            datePicker1.DisplayDate = DateTime.Now;
            datePicker1.Text = DateTime.Now.ToShortDateString();
        }

        private void datePicker1_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            DataTable tb = db.getTable("select * from Blocks where ValidUpTo=(select MIN(ValidUpTo) from Blocks where ValidUpTo>='" + datePicker1.Text + "')");
            blocksGridControl.DataSource = tb;
        }

        public void LoadDataByDate(DateTime tm)
        {
            DataTable tb = db.getTable("select * from Blocks where ValidUpTo=(select MIN(ValidUpTo) from Blocks where ValidUpTo>='" + tm.ToShortDateString() + "')");
            blocksGridControl.DataSource = tb;
        }

        public void editRecord(string searchwordAdv)
        {
            int selectedRowhandle = blocksGridControl.View.GetSelectedRowHandles()[0];
            object id = blocksGridControl.GetCellValue(selectedRowhandle, "id");

            BlockAdd iw = new BlockAdd();
            iw.red = true;
            Functionality.updateBlockIntoDB(iw, blocksGridControl, searchwordAdv, id.ToString());
            iw.red = false;

            DataTable tb = db.getTable("select * from Blocks where ValidUpTo=(select MIN(ValidUpTo) from Blocks where ValidUpTo>='" + datePicker1.Text + "')");
            blocksGridControl.DataSource = tb;
        }

        public void deleteRecord()
        {
            int selectedRowhandle = blocksGridControl.View.GetSelectedRowHandles()[0];
            object id = blocksGridControl.GetCellValue(selectedRowhandle, "id");
            db.sqlExecute("delete from Blocks where id=" + id.ToString());
            DataTable tb = db.getTable("select * from Blocks where ValidUpTo=(select MIN(ValidUpTo) from Blocks where ValidUpTo>='" + datePicker1.Text + "')");
            blocksGridControl.DataSource = tb;
        }
    }
}
